#!/bin/sh

EVENTS="game-selected system-selected game-start game-end screensaver-start screensaver-stop achievements"
PIDFILE=/var/run/dmd_simulator.pid

start() {
        echo -n "Starting dmd-simulator: "

	# add hooks
        for EVT in ${EVENTS}
        do
            mkdir -p /var/run/emulationstation/scripts/${EVT} || exit 1
            ln -sf /usr/share/dmd-simulator/scripts/${EVT}.sh /var/run/emulationstation/scripts/${EVT}/dmd-simulator.sh || exit 1
        done

	DMDFORMAT=$(batocera-settings-get dmd.format)
	DMDARGS=
	if test "${DMDFORMAT}" = "hd"
	then
	    DMDARGS="--width 256 --height 64"
	fi

        start-stop-daemon -S -b -q -m -p $PIDFILE --exec /usr/bin/dmd-simulator -- ${DMDARGS} >/dev/null &
        RETVAL=$?

	echo "done"
        return $RETVAL
}

stop() {
        echo -n "Stopping dmd-simulator: "

        # remove hooks
        for EVT in ${EVENTS}
        do
            unlink /var/run/emulationstation/scripts/${EVT}/dmd-simulator.sh
        done

        start-stop-daemon -K -q -p $PIDFILE
	RETVAL=$?
	
        echo "done"
        return $RETVAL
}

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        *)
                echo "Usage: $0 {start|stop}"
                exit 1
esac

exit $?
